package com.infinit.easyreader.framework;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.infinit.easyreader.R;

/**
 * 类：com.infinit.easyreader.framework.TabView V[1.0.0]
 * 作用：部分下方按钮的view，包含一个图片和一个按钮
 * Created by rzy on 2016/5/17.
 */
public class TabView extends View {
    /**
     * 定义该View的最小尺寸
     */
    private int minSize;
    /**
     * 图片的bitmap
     */
    private Bitmap imgBitmap;

    /**
     * 图片的左padding
     */
    private int imgPaddingLeft;
    /**
     * image's right padding
     */
    private int imgPaddingRight;
    /**
     * image's paddingTop
     */
    private int imgPaddingTop;
    /**
     * images' paddingBottom
     */
    private int imgPaddingBottom;
    /**
     * title text
     */
    private String titleText;
    /**
     * title text color
     */
    private int titleColor;
    /**
     * title text size
     */
    private int titleSize;
    /**
     * title padding left
     */
    private int titlePaddingLeft;
    /**
     * title padding right
     */
    private int titlePaddingRight;
    /**
     * title padding top
     */
    private int titlePaddingTop;
    /**
     * title padding bottom
     */
    private int titlePaddingBottom;
    private TextPaint mTextPaint;
    private Rect rect;
    private int mViewHeight;
    private int mViewWidth;
    private Paint mPaint;


    public TabView(Context context) {

        this(context, null);

    }

    public TabView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        minSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 28, getResources().getDisplayMetrics());
        rect = new Rect();
        mPaint = new Paint();
        mTextPaint = new TextPaint();
        //默认的title字体大小为8sp
        titleSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 9, getResources().getDisplayMetrics());
        //从布局中读取属性
        TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.TabView, defStyleAttr, 0);
        int n = array.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = array.getIndex(i);
            switch (attr) {
                case R.styleable.TabView_imageSrc:
                    imgBitmap = BitmapFactory.decodeResource(getResources(), array.getResourceId(attr, 0));
                    break;
                case R.styleable.TabView_imgPaddingLeft:
                    imgPaddingLeft = array.getDimensionPixelSize(attr, 0);
                    break;
                case R.styleable.TabView_imgPaddingRight:
                    imgPaddingRight = array.getDimensionPixelSize(attr, 0);
                    break;
                case R.styleable.TabView_imgPaddingTop:
                    imgPaddingTop = array.getDimensionPixelSize(attr, 0);
                    break;
                case R.styleable.TabView_imgPaddingBottom:
                    imgPaddingBottom = array.getDimensionPixelSize(attr, 0);
                    break;
                //获取标题的相关属性
                case R.styleable.TabView_titleText:
                    titleText = array.getString(attr);
                    break;
                case R.styleable.TabView_textSize:
                    titleSize = array.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 21, getResources().getDisplayMetrics()));
                    break;
                case R.styleable.TabView_textColor:
                    titleColor = array.getColor(attr, Color.BLACK);
                    break;
                case R.styleable.TabView_textPaddingLeft:
                    titlePaddingLeft = array.getDimensionPixelSize(attr, 0);
                    break;
                case R.styleable.TabView_textPaddingRight:
                    titlePaddingRight = array.getDimensionPixelSize(attr, 0);
                    break;
                case R.styleable.TabView_textPaddingTop:
                    titlePaddingTop = array.getDimensionPixelSize(attr, 0);
                    break;
                case R.styleable.TabView_textPaddingBottom:
                    titlePaddingBottom = array.getDimensionPixelSize(attr, 0);
                    break;
            }


        }
        array.recycle();
    }


    public TabView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //获取高度和宽度的相应数值
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int viewWidth = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int viewHeight = MeasureSpec.getSize(heightMeasureSpec);
        int width;
        int height;
        //处理宽度
        if (widthMode == MeasureSpec.EXACTLY) {
            width = viewWidth;
        } else {
            int desiredWidth = getPaddingLeft() + getPaddingRight() + imgPaddingLeft + imgPaddingRight;
            desiredWidth += (imgBitmap == null ? TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 28, getResources().getDisplayMetrics()) : imgBitmap.getWidth());
            width = Math.max(desiredWidth, minSize);
            if (widthMode == MeasureSpec.AT_MOST) {
                width = Math.min(desiredWidth, minSize);
            }
        }
        //处理高度
        if (heightMode == MeasureSpec.EXACTLY) {
            height = viewHeight;
        } else {
            int desiredHeight = getPaddingBottom() + getPaddingBottom() + width;
            if (titleText != null) {
                mTextPaint.setTextSize(titleSize);
                mTextPaint.setColor(titleColor);
                mTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
                Paint.FontMetrics fm = mTextPaint.getFontMetrics();
                int textHeight = (int) Math.ceil(fm.descent - fm.ascent);
                desiredHeight += (textHeight + titlePaddingTop + titlePaddingBottom);
            }

            height = Math.max(desiredHeight, minSize);
            if (heightMode == MeasureSpec.AT_MOST) {
                height = Math.min(desiredHeight, minSize);
            }
        }
        mViewHeight = height;
        mViewWidth = width;
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        rect.left = getPaddingLeft();
        rect.top = getPaddingTop();
        rect.right = mViewWidth - getPaddingRight();
        rect.bottom = mViewHeight - getPaddingBottom();
        //从下方往上方进行绘制
        mTextPaint.setTextSize(titleSize);
        mTextPaint.setColor(titleColor);
        mTextPaint.setTextAlign(Paint.Align.LEFT);
        //获取高度
        Paint.FontMetrics fm = mTextPaint.getFontMetrics();
        int textHeight = (int) Math.ceil(fm.descent - fm.ascent);
        float left = getPaddingLeft() + titlePaddingLeft;
        float right = rect.right - titlePaddingRight;
        float bottom = rect.bottom - titlePaddingBottom;
        //获取适合长度的字符串
        if (titleText != null) {
            String title = TextUtils.ellipsize(titleText, mTextPaint, right - left, TextUtils.TruncateAt.END).toString();
            float textWidth = mTextPaint.measureText(title);
            //标题居中
            float x = textWidth > (right - left) ? left : left + (right - left - textWidth) / 2;
            //绘制标题
            canvas.drawText(title, x, bottom - fm.descent, mTextPaint);
        }
        //绘制辅助矩形上移
        if (imgBitmap != null) {
            rect.bottom -= (textHeight + titlePaddingTop + titlePaddingBottom);
            //绘制图片
            mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
            rect.left += imgPaddingLeft;
            rect.top += imgPaddingTop;
            rect.right -= imgPaddingRight;
            rect.bottom -= imgPaddingBottom;

            //将图标置于矩形正中，并且将其缩放到合适的比例
            int desiredSize;
            if ((rect.right - rect.left) > (rect.bottom - rect.top)) {
                desiredSize = rect.bottom - rect.top;
                rect.left += (rect.right - rect.left - desiredSize) / 2;
                rect.right = rect.left + desiredSize;
            } else {
                desiredSize = rect.right - rect.left;
                rect.top += (rect.bottom - rect.top - desiredSize) / 2;
                rect.bottom = rect.top + desiredSize;
            }
            canvas.drawBitmap(imgBitmap, null, rect, mPaint);
        }
    }


}



